package com.luxiu.extensionlib.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewbinding.ViewBinding
import kotlin.reflect.KClass

abstract class BaseActivity <T : ViewBinding>(private val bindingClass: KClass<T>): AppCompatActivity() {

    protected lateinit var mContext: Context
    protected lateinit var mActivity: Activity
    lateinit var mHandler: Handler
    lateinit var mBinding: T


    @Suppress("UNCHECKED_CAST")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        mContext = this
        mActivity = this
        mHandler = Handler(mainLooper)
        mBinding = bindingClass.java.getDeclaredMethod("inflate", LayoutInflater::class.java).invoke(null, layoutInflater) as T
        setContentView(mBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(mBinding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        initBinding()
        initObserver()
        initView()
    }

    abstract fun initBinding()

    abstract fun initObserver()

    abstract fun initView()

    fun back(toolbar: Toolbar) {
        toolbar.setNavigationOnClickListener {
            mActivity.finish()
        }
    }

    fun back(view: View) {
        view.setOnClickListener {
            mActivity.finish()
        }
    }
}

